function get_install_tar_file(){
  file_name="$1/"$(ls -1l "${DIR}/$1" | sed -n '2,$p' | grep -v "^d" | grep -vE "(.conf|.service)$" | awk '{print $NF}')
}

function get_tar_default_folder_name(){
  folder_name=$(tar -tf $1 | awk -F '/' '{print $1}' | sort | uniq )
}
install_jdk(){
  module_name="java"
  DIR=$( dirname "$(readlink -f  ${BASH_SOURCE[0]})" )
  get_install_tar_file ${module_name}
  echo ${file_name%%.tar.gz}
  get_tar_default_folder_name "${DIR}/$file_name"
  if [[ -d /usr/local/${module_name} ]]
  then
    sudo rm -rf /usr/local/${module_name}
  fi
  sudo tar -zxvf ${DIR}/${file_name} -C /usr/local/
  sudo ln -sf /usr/local/${folder_name} /usr/local/${module_name}

  if [ ! -e "${HOME}/.${USER}/profile" ]
  then
    mkdir ${HOME}/.${USER}
    echo '#!bin/bash' > ${HOME}/.${USER}/profile
  fi
  grep -q 'export JAVA_DEFAULT_HOME=/usr/local/java' ~/.${USER}/profile
  if [ $? -eq 1 ];then
  sudo sed -i'' '$aexport JAVA_DEFAULT_HOME=/usr/local/java' ~/.${USER}/profile
  sudo sed -i'' '$aif [[ -z $JAVA_HOME ]];then\necho "set default jdk ${JAVA_DEFAULT_HOME}"\nexport JAVA_HOME=${JAVA_DEFAULT_HOME}\nfi' ~/.${USER}/profile
  fi
  grep -q 'export JRE_HOME=${JAVA_HOME}/jre' ~/.${USER}/profile
  if [ $? -eq 1 ];then
  sudo sed -i'' '$aexport JRE_HOME=${JAVA_HOME}/jre' ~/.${USER}/profile
  fi
  grep -q 'export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib' ~/.${USER}/profile
  if [ $? -eq 1 ];then
  sudo sed -i'' '$aexport CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib' ~/.${USER}/profile
  fi
  grep -q 'export PATH=${JAVA_HOME}/bin:${PATH}' ~/.${USER}/profile
  if [ $? -eq 1 ];then
  sudo sed -i'' '$aexport PATH=${JAVA_HOME}/bin:${PATH}:${HOME}/.local/bin' ~/.${USER}/profile
  fi
  #刷新环境变量
  grep -q 'source '"${HOME}"'/.'"${USER}"'/profile' /etc/profile
  if [ $? -eq 1 ];then
  sudo sed -i'' '$asource '"${HOME}"'/.'"${USER}"'/profile' /etc/profile
  fi

}
install_zk(){
  DIR=$( dirname "$(readlink -f  ${BASH_SOURCE[0]})" )
  get_install_tar_file zookeeper
  echo ${file_name%%.tar.gz}
  if [[ ! -d /usr/local/zookeeper ]]
  then
    sudo mkdir -p /usr/local/zookeeper
  fi
  sudo tar -zxvf ${DIR}/${file_name} -C /usr/local/zookeeper
  cd /usr/local/zookeeper/${file_name%%.tar.gz}
  sudo mkdir -p data
  cp ./conf/zoo_sample.cfg ./conf/zoo.cfg
  sudo sed -i 's#dataDir.*#/usr/local/zookeeper/'"${file_name%%.tar.gz}"'/data#g' ./conf/zoo.cfg
}

install_kafka(){
  module_name="kafka"
  DIR=$( dirname "$(readlink -f  ${BASH_SOURCE[0]})" )
  get_install_tar_file ${module_name}
  echo ${file_name%%.tar.gz}
  get_tar_default_folder_name "${DIR}/$file_name"
  if [[ -d /usr/local/${module_name} ]]
  then
    sudo rm -rf /usr/local/${module_name}
  fi
  sudo tar -zxvf ${DIR}/${file_name} -C /usr/local/
  sudo ln -sf /usr/local/${folder_name} /usr/local/${module_name}
  sudo cp -rvf ${DIR}/${module_name}/kafka.service /etc/systemd/system/
  sudo cp -rvf ${DIR}/${module_name}/zookeeper.service /etc/systemd/system/
  sudo chmod -R 777 /usr/local/kafka
  sudo systemctl enable zookeeper
  sudo systemctl enable kafka
  sudo systemctl restart zookeeper
  sudo systemctl restart kafka
}

install_redis(){
	yum -y install gcc zlib-devel openssl openssl-developenssl openssl-devel curl curl-devel
  module_name="redis"
  DIR=$( dirname "$(readlink -f  ${BASH_SOURCE[0]})" )
  get_install_tar_file ${module_name}
  echo ${file_name%%.tar.gz}
  get_tar_default_folder_name "${DIR}/$file_name"
  if [[ ! -d /usr/local/${module_name} ]]
  then
    sudo mkdir -p /usr/local/${module_name}
  fi
  sudo tar -zxvf ${DIR}/${file_name} -C /usr/local/
  # sudo ln -sf /usr/local/${folder_name} /usr/local/${module_name}
  cd /usr/local/${folder_name}
  sudo make
  sudo make install PREFIX=/usr/local/redis 
  sudo cp -rvf ${DIR}/${module_name}/redis.service /etc/systemd/system/
  if [[ ! -d /etc/redis ]]
  then
    sudo mkdir -p /etc/redis
  fi
  sudo cp -rvf ${DIR}/${module_name}/redis.conf /etc/redis/
  sudo systemctl enable redis
  sudo systemctl restart redis
}

install_minio(){
  module_name="minio"
  DIR=$( dirname "$(readlink -f  ${BASH_SOURCE[0]})" )
  sh ${DIR}/minio/install_minio/install_sh/minio_install_el.sh
  sh ${DIR}/minio/install_minio/install_sh/start.sh
  sh ${DIR}/minio/install_minio/install_sh/mc_config.sh
}

install_nacos(){
  module_name="nacos"
  DIR=$( dirname "$(readlink -f  ${BASH_SOURCE[0]})" )
  get_install_tar_file ${module_name}
  echo ${file_name%%.tar.gz}
  get_tar_default_folder_name "${DIR}/$file_name"
  if [[ -d /usr/local/${module_name} ]]
  then
    sudo rm -rf /usr/local/${module_name}
  fi
  sudo tar -zxvf ${DIR}/${file_name} -C /usr/local/
  sudo ln -sf /usr/local/${folder_name} /usr/local/${module_name}
  sudo chmod -R 777 /usr/local/${module_name} 
  sh /usr/local/nacos/bin/startup.sh -m standalone
}

install_nginx(){
  module_name="nginx"
  DIR=$( dirname "$(readlink -f  ${BASH_SOURCE[0]})" )
  sudo yum -y install nginx
  sudo chmod -R 777 /var/log/nginx
  sudo mkdir -p /var/www/cache/tmp
  sudo chmod -R 777 /var/www/cache
  sudo cp -rvf ${DIR}/nginx/* /etc/nginx/
}
install_services(){
  module_name="services"
  DIR=$( dirname "$(readlink -f  ${BASH_SOURCE[0]})" )
  sudo tar -zxvf ${DIR}/offline/services.tar.gz -C /opt/apexapp/
  sudo tar -zxvf ${DIR}/offline/local.tar.gz -C /opt/apexapp/
  sudo tar -zxvf ${DIR}/offline/web.tar.gz -C /opt/apexapp/
}
install_crontab(){
  module_name="crontab"
  DIR=$( dirname "$(readlink -f  ${BASH_SOURCE[0]})" )
  get_install_tar_file ${module_name}
  crontab ${DIR}/${file_name}
}
install_$1
